//
//  ST_SwiftyFloatingWidget.swift
//
//  Created by Jason on 2021/8/10.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//
import UIKit


public class ST_SwiftyFloatingWidget {
    
    
    public static let provider = ST_SwiftyFloatingWidget()
  
    // Can't init is singleton
    private init() {}
    
    fileprivate(set) var sharePreView:UIView?
    
    public fileprivate(set) var currentWindow:UIWindow?
    
    fileprivate(set) var temporaryController:ST_ST_SwiftyFloatingWidgetProtocol?
        
}


public extension ST_SwiftyFloatingWidget{
 
    func floating(config:ST_SwiftyWidgetConfig = .default,controller:ST_ST_SwiftyFloatingWidgetProtocol){
        
        
        ST_SwiftyWidgetConfig.default = config
        
        temporaryController = controller
        
        let toFrame = ST_SwiftyWidgetConfig.default.window_default_frame.inset(by: ST_SwiftyWidgetConfig.default.window_content_edge)
        
        controller.swiftyPrepareFloat(toFrame: toFrame)
                
        UIView.animate(withDuration: ST_SwiftyWidgetConfig.default.animationDuration, animations: {
            
            controller.swiftyFloatAnimating(toFrame: toFrame)
            
        }) { (complete) in
            
            let animateShareView = controller.swiftyFloatingShareView()
            
            self.sharePreView = animateShareView
            
            let window = self.createFloatingWindow()
                
            window.show()
            window.addSubview(animateShareView)
            
            animateShareView.translatesAutoresizingMaskIntoConstraints = false
            animateShareView.topAnchor.constraint(equalTo: window.topAnchor, constant: ST_SwiftyWidgetConfig.default.window_content_edge.top).isActive = true
            animateShareView.bottomAnchor.constraint(equalTo: window.bottomAnchor, constant: -ST_SwiftyWidgetConfig.default.window_content_edge.bottom).isActive = true
            animateShareView.leadingAnchor.constraint(equalTo: window.leadingAnchor, constant: ST_SwiftyWidgetConfig.default.window_content_edge.left).isActive = true
            animateShareView.trailingAnchor.constraint(equalTo: window.trailingAnchor, constant: -ST_SwiftyWidgetConfig.default.window_content_edge.right).isActive = true
            
            
            self.currentWindow = window
            
            controller.swiftyFloatAnimationComplete()
            
        }
    }
    
    
    func destroy(){
        
        self.sharePreView = nil
        self.currentWindow?.isHidden = true
        self.currentWindow = nil
        self.temporaryController = nil
    }
    
}




extension ST_SwiftyFloatingWidget{
    
    
    fileprivate func createFloatingWindow()->ST_SwiftyFloatingWindow{
        
        var window:ST_SwiftyFloatingWindow
        
        if #available(iOS 13, *) {
            if let windowScene = ST_SwiftyFloatingWindow.keyWindowScene(){
                window = ST_SwiftyFloatingWindow(windowScene: windowScene)
                window.frame = CGRect(origin: ST_SwiftyWidgetConfig.default.floatingDefaultPoint, size: ST_SwiftyWidgetConfig.default.floatingDefaultSize)
            }else{
                window = ST_SwiftyFloatingWindow(frame: CGRect(origin: ST_SwiftyWidgetConfig.default.floatingDefaultPoint, size: ST_SwiftyWidgetConfig.default.floatingDefaultSize))
            }
        } else {
            window = ST_SwiftyFloatingWindow(frame: CGRect(origin: ST_SwiftyWidgetConfig.default.floatingDefaultPoint, size: ST_SwiftyWidgetConfig.default.floatingDefaultSize))
        }
        
        
        window.layer.masksToBounds = true
        window.layer.cornerRadius = abs(CGFloat(Int(ST_SwiftyWidgetConfig.default.floatingWindowRadious * 100)) / 100)
        window.backgroundColor = ST_SwiftyWidgetConfig.default.window_background_color
        
        window.tap_recover_closure = { [weak self] in
            self?.window_tap_handle()
        }
        
        return window
    }
    
    
    fileprivate func window_tap_handle(){
        
        
        
        guard let window = currentWindow,self.temporaryController?.swiftyFloatingCanRecover() == true else { return  }
        
        sharePreView?.removeFromSuperview()
        window.isHidden = true
        
        let toFrame = window.frame.inset(by: ST_SwiftyWidgetConfig.default.window_content_edge)
        
        self.temporaryController?.swiftyPrepareRecover(recoverRect: toFrame, completion: {
            
            UIView.animate(withDuration: ST_SwiftyWidgetConfig.default.animationDuration, animations: {

                self.temporaryController?.swiftyRecovering()
                
            }) { _ in
                
                self.temporaryController?.swiftyRecoverComplete()

                self.destroy()
                
            }
            
        })
        
        
        
    }
    
    
    
}
